package com.selftool.gui.superframe;

import com.selftool.base.NCFrame;
import com.selftool.gui.devframe.DevFrom;
import com.selftool.gui.serverframe.MainFrom;
import com.selftool.gui.superframe.action.DevButton;
import com.selftool.gui.superframe.action.ServerButton;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;

/**
 * @program: selftool
 * @description: GUI主框架
 * @author: CYQ
 * @create: 2024-02-21 10:12
 **/
@Component
public class SuperFrame extends NCFrame{
    private JPanel superPanel = new JPanel();
    @Autowired
    private ServerButton serverBt;
    @Autowired
    private DevButton devBt;

//    @PostConstruct
    public JFrame create(){
        JFrame frame = createFrame("NCC自助工具");

        //注册工具条
//        NCButton serverBt = new ServerButton();
        serverBt.setText("服务管理");
//        NCButton devBt = new DevButton();
        devBt.setText("研发管理");
        JToolBar bar = new JToolBar("工具条", SwingConstants.HORIZONTAL);
        bar.add(serverBt);
        bar.add(devBt);

        // 使用BorderLayout设置superPanel的布局管理器
        superPanel.setLayout(new BorderLayout());
        bar.setLayout(new FlowLayout(FlowLayout.LEFT));
        superPanel.add(bar, BorderLayout.NORTH);
        superPanel.setLayout(new BoxLayout(superPanel, BoxLayout.Y_AXIS));

        MainFrom serverframe = new MainFrom();
        DevFrom devframe = new DevFrom();

        //注册服务管理
        addPanel(serverframe);
        //注册研发管理
        addPanel(devframe);

        showPanel(0);
        frame.setContentPane(superPanel);
        frame.setVisible(true);
        return frame;
    }
//    private
    private JFrame createFrame(String name) {
        JFrame frame = new JFrame(name);
        int INIT_W = 800;  //窗体初始宽度
        int INIT_H = 600;  //窗体初始高度
        //窗口居中显示
        Point p = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
        frame.setBounds(p.x - INIT_W / 2, p.y - INIT_H / 2, INIT_W, INIT_H);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(INIT_W,INIT_H);
        return frame;
    }
    /** 页面列表 */
    private static ArrayList<JPanel> list = new ArrayList();
    /** 显示页面序号 */
    private static Integer showPanelIndex = 0;
    /** 增加页面 */
    private void addPanel(NCFrame frame){
        JPanel panel = frame.getMain();
        panel.setVisible(false);
        superPanel.add(panel,BorderLayout.CENTER);
        list.add(panel);
    }
    /** 显示指定页面 */
    public static void showPanel(Integer show){
        list.get(showPanelIndex).setVisible(false);
        showPanelIndex = show;
        list.get(showPanelIndex).setVisible(true);
    }

    /**
     * @return
     */
    @Override
    public JPanel getMain() {
        return superPanel;
    }
}
